package com.joy.config;

import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.template.Engine;
import com.joy.controller.IndexController;
import com.joy.model.User;
import com.joy.routes.AdminRoute;
import com.joy.routes.FrontRoute;

public class MyConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", IndexController.class);

        me.add(new FrontRoute());//前台路由
        me.add(new AdminRoute());//后台路由
    }

    @Override
    public void configEngine(Engine me) {

    }

    @Override
    public void configPlugin(Plugins me) {
        PropKit.use("jdbc.properties");
        final String URL = PropKit.get("jdbcUrl");
        final String USERNAME = PropKit.get("user");
        final String PASSWORD = PropKit.get("password");
        final Integer INITIALPOOLSIZE = PropKit.getInt("initialPoolSize");
        final Integer MAXIDLETIME = PropKit.getInt("maxIdleTime");
        final Integer MINPOOLSIZE = PropKit.getInt("minPoolSize");
        final Integer MAXPOOLSIZE = PropKit.getInt("maxPoolSize");

        C3p0Plugin c3p0Plugin = new C3p0Plugin(URL, USERNAME, PASSWORD);
        c3p0Plugin.setInitialPoolSize(INITIALPOOLSIZE);
        c3p0Plugin.setMaxIdleTime(MAXIDLETIME);
        c3p0Plugin.setMaxPoolSize(MAXPOOLSIZE);
        c3p0Plugin.setMinPoolSize(MINPOOLSIZE);
        me.add(c3p0Plugin);


        //注册activeRecord插件，配置数据库映射
        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(c3p0Plugin);
        //添加model类和数据库表的映射，user指的是表名，userid 指的是主键
        //表的主键名默认是id，所以需要手动配置
        activeRecordPlugin.addMapping("user", "userid", User.class);
        me.add(activeRecordPlugin);

    }

    @Override
    public void configInterceptor(Interceptors me) {

    }

    @Override
    public void configHandler(Handlers me) {

    }
}
